/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : May 11, 2012  11:55:39 AM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.business.process.commons.project
 * File Name : ProjectProcessing.java
 * TODO
 */
package apps.business.process.commons.project;

import java.util.List;

import apps.business.process.SyncProcessorCommons;
import jpa.entitys.promis.common.CostProject;
import jpa.entitys.synchro.staging.PsProjDef;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import promis.utility.ProcessLevel;
import sap.business.mm.outbound.PsProjDefService;
import apps.promis.in.common.ProjectAction;
import apps.promis.out.common.ProjectOutAction;
import apps.promis.out.common.SyncQueueAction;

/**
 * @author chatree
 *
 */
public class ProjectProcessing extends SyncProcessorCommons {
	
	private static final Logger log = Logger.getLogger(ProjectProcessing.class);

	/**
	 * 
	 */
	public ProjectProcessing() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 
	 * @param sap_plant_code
	 * @throws Exception
	 */
	public void Processing(String sap_plant_code) throws Exception {
		try{
			
			UPD_PROMISUseTime(true, false, null);
			//String sap_plant_code = "1116";
			List<PsProjDef> psProjectList = new ProjectOutAction().CreatedProjToStagingPlant(sap_plant_code);
			System.out.println("Create Project Defination to Staging [sap_plant_code = "+ sap_plant_code +"] Size=" + CommonService.getSize(psProjectList));
			UPD_PROMISUseTime(false, true, null);
			
			
			//Send to SAP
			if(CommonService.getSize(psProjectList) > 0){
				UPD_SENDUseTime(true, false, psProjectList.get(0).getInstanceNo());
				new PsProjDefService().SendingToSAP(psProjectList);		
				UPD_SENDUseTime(false, true, psProjectList.get(0).getInstanceNo());	
			}
			System.out.println("Sending Project Defination to SAP Size=" + CommonService.getSize(psProjectList));
			
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	public void ProcessingByProjectCode(String company, String projectCode) throws Exception {
		try{
			
			UPD_PROMISUseTime(true, false, null);
			//String sap_plant_code = "1116";
			List<PsProjDef> psProjectList = new ProjectOutAction().CreatedProjToStaging(company, projectCode);
			System.out.println("Create Project Defination to Staging size=" + CommonService.getSize(psProjectList));
			UPD_PROMISUseTime(false, true, null);
			
			if(CommonService.getSize(psProjectList) > 0){
				UPD_SENDUseTime(true, false, psProjectList.get(0).getInstanceNo());
				new PsProjDefService().SendingToSAP(psProjectList);				
				UPD_SENDUseTime(false, true, psProjectList.get(0).getInstanceNo());
			}	
			System.out.println("Sending Project Defination to SAP size=" + CommonService.getSize(psProjectList));
			
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	public void ProcessingAll() throws Exception {
		log.info("Start ProcessingAll --->");
		System.out.println("Start ProcessingAll --->");
		try{
			List<CostProject> lst1 = new ProjectAction().findAll();
			log.info("Project List : " + CommonService.getSize(lst1));
			System.out.println("Project List : " + CommonService.getSize(lst1));
			
			int i = 0;
			if(CommonService.getSize(lst1) > 0){
				for(CostProject model : lst1){
					try{
						
						ProcessingByProjectCode(model.getId().getCompany(), model.getId().getProject());
						System.out.println("i="+i + " || Company=" + model.getId().getCompany() + " || Project=" + model.getId().getProject());
						
					}catch(Exception e){
						log.error(e.getMessage());
						System.err.println(e.getMessage());
					}
				}
			}
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}finally{
			log.info("End ProcessingAll --->");
			System.out.println("End ProcessingAll --->");
		}
	}
	
	/**
	 * In Staging Only
	 * @throws Exception
	 */
	public void ProcessingNotSyncInStaging() throws Exception {
		try{
			
			List<Object[]> lst = new ProjectOutAction().SearchNotSyncInStaging();
			System.out.println("ProjectOutAction size=" + CommonService.getSize(lst));
			log.info("ProjectOutAction size=" + CommonService.getSize(lst));
			
			if(CommonService.getSize(lst) > 0){
				for(Object[] value : lst){
					try{
						
						String projectCode = value[0].toString();					
						String instanceNo = value[1].toString();
						
						System.out.println("projectCode=" + projectCode + " || instanceNo=" +instanceNo);
						List<PsProjDef> psProjectList = new ProjectOutAction().findByInstanceNo(projectCode, instanceNo);
						
						//Sending to SAP
						new PsProjDefService().SendingToSAP(psProjectList);
						log.info("PsProjDef Sending to SAP size="+ CommonService.getSize(psProjectList));
						
					}catch(Exception e){
						log.error(e.getMessage());
						System.err.println(e.getMessage());
					}
				}
			}
			
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
	}
	
	@Deprecated
	private void Start() throws Exception {
		try{
			
			String sap_plant_code = "1116";
			
			log.info("Start Create Project Defination to Staging ");
			List<PsProjDef> psProjectList = new ProjectOutAction().CreatedProjToStagingPlant(sap_plant_code);
			log.info("End Create Project Defination to Staging ");
			
			log.info("Start Sending Project Defination to SAP ");
			new PsProjDefService().SendingToSAP(psProjectList);
			log.info("End Sending Project Defination to SAP ");
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try{
			
			new ProjectProcessing().ProcessingAll();
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
	}
}
